<?php
/**
 * Created by 广告类.
 * User: 尚金殿
 * Date: 2018/8/27
 * Time: 14:51
 */

namespace app\admin\controller;


use think\Db;

class AdvertController extends CommonController
{
    protected $mysql = 'advert';
    protected $pk = 'advert_id';
    protected $status = 'status';


    //添加广告
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->param();

            //判定数据
            if(!$data['type_id'] || !is_numeric($data['type_id'])){
                return show(0,'广告分类不能为空');
            }

            if(!$data['cover']){
                return show(0,'广告图片不能为空');
            }
            if(!empty($data['advert_id'])){
                $this->save($data);
            }
            //入库
            $advertId = $this->getAdd($data);

            if(!$advertId){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            //获取广告分类
            $type = Db::name('advert_type')->where('type_status',1)->select();

            //变量输出
            $this->assign('type',$type);

            //加载模版
            return $this->fetch();

        }
    }

    //广告编辑
    public function edit(){
        $id = $this->request->param('id');

        //获取广告分类
        $type = Db::name('advert_type')->where('type_status',1)->select();

        //获取广告
        $advert = Db::name($this->mysql)->where($this->pk,$id)->find();

        //变量输出
        $this->assign(['type'=>$type,'advert'=>$advert]);

        //加载模版
        return $this->fetch();
    }

    //保存编辑
    public function save($data){
        //判定传递的数据
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }
        //获取ID
        $id = $data['advert_id'];
        unset($data['advert_id']);


        $data['update_time'] = time();

        //入库
        $res = Db::name($this->mysql)->where($this->pk,$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
        return show(1,'更新成功');

    }











    /**************************************广告分类***************************************/

    //广告分类列表
    public function typeLists(){
        //获取广告分类
        $res = Db::name('advert_type')->where('type_status','>',-1)->paginate(10);
        //变量输出
        $this->assign('res',$res);
        //加载模版
        return $this->fetch();
    }

    //新增广告分类
    public function typeAdd(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();

            //判定数据
            if(!$data['type_name']){
                return show(0,'广告分类不能为空');
            }

            if(!empty($data['type_id'])){
                $this->typeSave($data);
            }
            $data['create_time'] = $data['update_time'] = time();
            //入库
            $res = Db::name('advert_type')->insertGetId($data);

            if(!$res){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            return $this->fetch();
        }
    }

    //编辑广告分类
    public function typeEdit(){
        $id = $this->request->param('id');

        //获取数据
        $type = Db::name('advert_type')->where('type_id',$id)->find();

        //变量输出
        $this->assign('type',$type);

        //加载模版
        return $this->fetch();
    }

    //保存编辑操作
    public function typeSave($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }

        $id = $data['type_id'];
        unset($data['type_id']);

        $data['update_time'] = time();

        $res = Db::name('advert_type')->where('type_id',$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
            return show(1,'更新成功');
    }


    //删除分类
    public function typeSetStatus(){
        $id = $this->request->param('id');
        $data['type_status'] = $this->request->param('status');
        $data['update_time'] = time();

        $res = Db::name('advert_type')->where('type_id',$id)->update($data);

        if(!$res){
            return show(0,'操作失败');
        }
        return show(1,'操作成功');
    }
}